<?php
//header("Content-type: text/ xml");
/**
 * @file
 * User has successfully authenticated with Twitter. Access tokens saved to session and DB.
 */

/* Load required lib files. */
//session_start();
	require_once('twitteroauth/twitteroauth.php');
	require_once('config.php');

	function getConnectionWithAccessToken($oauth_token, $oauth_token_secret) 
	{
  		$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $oauth_token, $oauth_token_secret);
  		return $connection;
	}


	function GetTwitterMedia( $GET )
	{
		$connection = getConnectionWithAccessToken( $GET['Access_token'], $GET['Access_token_secret'] );
		//var_dump($connection);
		//$connection = getConnectionWithAccessToken($GET['Access_token'], $GET['Access_token']);
		//$connection = getConnectionWithAccessToken("744201864-agn4IQ8yJ3O6CKa2NhbNca7Ow6UvWAJC9SgqEPZG", "Q5FNaKwH3HtTkHy34lbOQOnuosbD0cNgnRR1ptglo0E");
		//var_dump($connection);
	

		if (($GET['Reset']=='true')||(empty($_SESSION['max_id'])))
		{
			//session_unset();
			$query = $connection->get("https://api.twitter.com/1/statuses/home_timeline.json?count=".$GET['twitter_count']."");
			
			if( isset($query->error) )
			{
				return Array();
			}
			
			/*******Test Limit
			$Rate_Limit = $connection->get("https://api.twitter.com/1/account/rate_limit_status.json");
			echo 'Limit_____________________Test';
 			echo '<pre>';
			var_dump($Rate_Limit);
			echo '</pre>';
			/********/
	
			if( isset($query->errors) )
			{
				echo $query->errors[0]->message;
				return;
			}
	
			/*******Test
			echo '<pre>';
			var_dump($query);
			echo '</pre>';
			/********/
	
			
	
			$_SESSION['max_id'] = $query[count($query)-1]->id_str;
			$_SESSION['Since_id'] = $query[0]->id_str;
		}
		else
		{
			$query = $connection->get("https://api.twitter.com/1/statuses/home_timeline.json?since_id=".$_SESSION['Since_id']."&count=".$GET['twitter_count']."");
	
			if( isset($query->errors) )
			{
				echo $query->errors[0]->message;
				return;
			}
	
			$count_second = $GET['twitter_count'] - count($query);
			$query_Second = $connection->get("https://api.twitter.com/1/statuses/home_timeline.json?max_id=".$_SESSION['max_id']."&count=".$count_second);
	
			if( isset($query_Second->errors) )
			{
				echo $query_Second->errors[0]->message;
				return;
			}
	
			if (!empty($query))
				$_SESSION['Since_id'] = $query[0]->id_str;
		}

		$MediaList = array();
		foreach ($query as $object)
		{
			array_push ($MediaList, MakeMediaArray($object, $connection));
		}

		if(!empty($query_Second))
		{
			$_SESSION['max_id'] = $query_Second[count($query_Second)-1]->id_str;
			$i = 0;
			foreach ($query_Second as $object)
			{
				if($i!=0)
				{
					array_push ($MediaList, MakeMediaArray($object, $connection));
				}
				$i++;

			}
		}//End of IF
		return $MediaList;
	} // End of Function
	
	function MakeMediaArray ( $object, $connection)
	{

		$Media = array();
		$id_str = $object->id_str;
	
		//$query_2 = $connection->get("https://api.twitter.com/1/statuses/home_timeline.json?id=$id_str&include_entities=1&include_rts=1");

		//$entities = $object->entities;
	
		$text = $object->text;
		$screen_name = $object->user->screen_name;
		$user = $object->user->name;
		$created_at = $object->created_at;
	//$date = date("Y-m-d",strtotime($created_at));
	
	
		//$Description_text = substr($text,0,strpos($text, 'http'));
		$WebLink = "twitter.com/$screen_name/status/$id_str";
	//echo '<Media ID="'.$id_str.'" MediaType="" CommentCount="" WebLink="'.$WebLink.'" Source="Twitter" Author="'.$user.'" lastUpdate="'.$date.'" >';
		$MediaDetial = array( "Head"=>"Media", "ID"=>"", "BeMediaID"=>"Twitter_".$id_str, "MediaType"=>"", "CommentCount"=>"", "WebLink"=>$WebLink, "Source"=>"Twitter", "Author"=>$user,"lastUpdate"=>strtotime($created_at));
		array_push( $Media, $MediaDetial );
		//------------------------Media-text Info------------------------//
		//echo '<Text ID="'.$id_str.'" Title="" Description="'.$Description_text.'" />';
		$MediaDetial = array( "Head"=>"Text", "ID"=>$id_str, "Title"=>"", "Description"=>$text);
		array_push( $Media, $MediaDetial );
		//------------------------Media-image Info------------------------//
		if(!empty($object->entities->media))
		{
			//$image_url = substr($query_2->entities->media[0]->media_url,7);
			$image_url = $object->entities->media[0]->media_url;
			//echo '<Image ID="'.$id_str.'" Type="URL" FileName="'.$image_url.'" Description="" />';
			$MediaDetial = array( "Head"=>"Image", "ID"=>$id_str, "Type"=>'URL', "FileName"=>$image_url, "Description"=>"" );
			array_push( $Media, $MediaDetial );
		}
		
		//------------------------Media-video Info------------------------//
		if(!empty($object->entities->urls))
		{
			$video_url = $object->entities->urls[0]->display_url;
			//echo '<Video ID="'.$id_str.'" Type="URL" FileName="'.$video_url.'" Description="" />';
			$MediaDetial = array( "Head"=>"Video", "ID"=>$id_str, "Type"=>'URL', "FileName"=>$video_url, "Description"=>"");
			array_push( $Media, $MediaDetial );		
		}
		return $Media;
	//echo '</Media>';		
	}
?>